package com.webserver.core;

import com.webserver.http.HttpServletRequest;
import com.webserver.http.HttpServletResponse;

import java.io.File;

/**
 * 用来完成处理请求的环节
 */
public class DispatcherServlet {

    public void service(HttpServletRequest request, HttpServletResponse response){
        String path =request.getUir();
        System.out.println("请求抽象路径："+path);

        File file =new File("./webapps"+path);


        if (file.isFile()){//请求的是一个存在的文件
            response.setContentFile(file);
            response.addHeader("Content-Type","text/html");
            response.addHeader("Content-Length",file.length()+"");
        }else {
            response.setStatusCode(404);
            response.setStatusReason("Not Found");
            file = new File("./webapps/root/404.html");
            response.setContentFile(file);
            response.addHeader("Content-Type","text/html");
            response.addHeader("Content-Length",file.length()+"");
        }
        response.addHeader("Server","WebServer");
    }
}
